import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { SystemRoleStateType } from './typings'
import {
  getUserRoleSelectList,
  getUserRoleList,
  postUserRoleSubmit,
  postUserRoleRemove,
  getUserRoleGrantTreeAdmin,
  getUserRoleTreeKeys,
  postUserRoleGrant
} from '~/api/system/systemRole'
import { filterValueEnum } from '~/utils/util'

const systemRole: Module<SystemRoleStateType, SystemRoleStateType> = {
  namespaced: true,
  state: {
    userRoleSelectList: [],
    userRoleEnum: {}
  },
  getters: {
    userRoleSelectList: (state) => state.userRoleSelectList,
    userRoleEnum: (state) => state.userRoleEnum
  },
  mutations: {
    SET_USER_ROLE_SELECT_LIST (state, payload) {
      state.userRoleSelectList = payload
      state.userRoleEnum = filterValueEnum(payload, 'id', 'title')
    }
  },
  actions: {
    // 获取当前租户的角色下拉列表
    async GetUserRoleSelectList<S, T> ({ commit }: ActionContext<S, T>, params: T) {
      return await getUserRoleSelectList<T>(params).then((res: AxiosResponse) => {
        commit('SET_USER_ROLE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },

    // 获取 角色管理 主列表
    GetUserRoleList<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getUserRoleList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 角色管理 新增、编辑
    PostUserRoleSubmit<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postUserRoleSubmit<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 删除角色
    PostUserRoleRemove<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postUserRoleRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 角色管理 权限设置(角色配置)
    GetUserRoleGrantTreeAdmin<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getUserRoleGrantTreeAdmin<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 角色管理 权限设置(角色配置) 已选中的
    GetUserRoleTreeKeys<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getUserRoleTreeKeys<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 角色管理 -> 权限设置 提交
    PostUserRoleGrant<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postUserRoleGrant<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default systemRole
